home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gspaint.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.7 KB  |  390 lines

  1. /* Copyright (C) 1989, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gspaint.c,v 1.2 2000/09/19 19:00:30 lpd Exp $ */
  20. /* Painting procedures for Ghostscript library */
  21. #include "math_.h"        /* for fabs */
  22. #include "gx.h"
  23. #include "gpcheck.h"
  24. #include "gserrors.h"
  25. #include "gsropt.h"        /* for gxpaint.h */
  26. #include "gxfixed.h"
  27. #include "gxmatrix.h"        /* for gs_state */
  28. #include "gspaint.h"
  29. #include "gspath.h"
  30. #include "gzpath.h"
  31. #include "gxpaint.h"
  32. #include "gzstate.h"
  33. #include "gxdevice.h"
  34. #include "gxdevmem.h"
  35. #include "gzcpath.h"
  36.  
  37. /* Define the nominal size for alpha buffers. */
  38. #define abuf_nominal_SMALL 500
  39. #define abuf_nominal_LARGE 2000
  40. #if arch_small_memory
  41. #  define abuf_nominal abuf_nominal_SMALL
  42. #else
  43. #  define abuf_nominal\
  44.      (gs_debug_c('.') ? abuf_nominal_SMALL : abuf_nominal_LARGE)
  45. #endif
  46.  
  47. /* Erase the page */
  48. int
  49. gs_erasepage(gs_state * pgs)
  50. {
  51.     /*
  52.      * We can't just fill with device white; we must take the
  53.      * transfer function into account.
  54.      */
  55.     int code;
  56.  
  57.     if ((code = gs_gsave(pgs)) < 0)
  58.     return code;
  59.     if ((code = gs_setgray(pgs, 1.0)) >= 0) {
  60.     /* Fill the page directly, ignoring clipping. */
  61.     code = gs_fillpage(pgs);
  62.     }
  63.     gs_grestore(pgs);
  64.     return code;
  65. }
  66.  
  67. /* Fill the page with the current color. */
  68. int
  69. gs_fillpage(gs_state * pgs)
  70. {
  71.     gx_device *dev;
  72.     int code;
  73.     gs_logical_operation_t save_lop;
  74.  
  75.     gx_set_dev_color(pgs);
  76.     dev = gs_currentdevice(pgs);
  77.     /* Fill the page directly, ignoring clipping. */
  78.     /* Use the default RasterOp. */
  79.     save_lop = pgs->log_op;
  80.     gs_init_rop(pgs);
  81.     code = gx_fill_rectangle(0, 0, dev->width, dev->height,
  82.                  pgs->dev_color, pgs);
  83.     pgs->log_op = save_lop;
  84.     if (code < 0)
  85.     return code;
  86.     return (*dev_proc(dev, sync_output)) (dev);
  87. }
  88.  
  89. /*
  90.  * Determine the number of bits of alpha buffer for a stroke or fill.
  91.  * We should do alpha buffering iff this value is >1.
  92.  */
  93. private int
  94. alpha_buffer_bits(gs_state * pgs)
  95. {
  96.     gx_device *dev;
  97.  
  98.     if (!color_is_pure(pgs->dev_color))
  99.     return 0;
  100.     dev = gs_currentdevice_inline(pgs);
  101.     if (gs_device_is_abuf(dev)) {
  102.     /* We're already writing into an alpha buffer. */
  103.     return 0;
  104.     }
  105.     return (*dev_proc(dev, get_alpha_bits))
  106.     (dev, (pgs->in_cachedevice ? go_text : go_graphics));
  107. }
  108. /*
  109.  * Set up an alpha buffer for a stroke or fill operation.  Return 0
  110.  * if no buffer could be allocated, 1 if a buffer was installed,
  111.  * or the usual negative error code.
  112.  *
  113.  * The fill/stroke code sets up a clipping device if needed; however,
  114.  * since we scale up all the path coordinates, we either need to scale up
  115.  * the clipping region, or do clipping after, rather than before,
  116.  * alpha buffering.  Either of these is a little inconvenient, but
  117.  * the former is less inconvenient.
  118.  */
  119. private int
  120. scale_paths(gs_state * pgs, int log2_scale_x, int log2_scale_y, bool do_path)
  121. {
  122.     /*
  123.      * Because of clip and clippath, any of path, clip_path, and view_clip
  124.      * may be aliases for each other.  The only reliable way to detect
  125.      * this is by comparing the segments pointers.  Note that we must
  126.      * scale the non-segment parts of the paths even if the segments are
  127.      * aliased.
  128.      */
  129.     const gx_path_segments *seg_clip =
  130.     (pgs->clip_path->path_valid ? pgs->clip_path->path.segments : 0);
  131.     const gx_clip_rect_list *list_clip = pgs->clip_path->rect_list;
  132.     const gx_path_segments *seg_view_clip;
  133.     const gx_clip_rect_list *list_view_clip;
  134.     const gx_path_segments *seg_effective_clip =
  135.     (pgs->effective_clip_path->path_valid ?
  136.      pgs->effective_clip_path->path.segments : 0);
  137.     const gx_clip_rect_list *list_effective_clip =
  138.     pgs->effective_clip_path->rect_list;
  139.  
  140.     gx_cpath_scale_exp2_shared(pgs->clip_path, log2_scale_x, log2_scale_y,
  141.                    false, false);
  142.     if (pgs->view_clip != 0 && pgs->view_clip != pgs->clip_path) {
  143.     seg_view_clip =
  144.         (pgs->view_clip->path_valid ? pgs->view_clip->path.segments : 0);
  145.     list_view_clip = pgs->view_clip->rect_list;
  146.     gx_cpath_scale_exp2_shared(pgs->view_clip, log2_scale_x, log2_scale_y,
  147.                    list_view_clip == list_clip,
  148.                    seg_view_clip && seg_view_clip == seg_clip);
  149.     } else
  150.     seg_view_clip = 0, list_view_clip = 0;
  151.     if (pgs->effective_clip_path != pgs->clip_path &&
  152.     pgs->effective_clip_path != pgs->view_clip
  153.     )
  154.     gx_cpath_scale_exp2_shared(pgs->effective_clip_path, log2_scale_x,
  155.                    log2_scale_y,
  156.                    list_effective_clip == list_clip ||
  157.                    list_effective_clip == list_view_clip,
  158.                    seg_effective_clip &&
  159.                    (seg_effective_clip == seg_clip ||
  160.                     seg_effective_clip == seg_view_clip));
  161.     if (do_path) {
  162.     const gx_path_segments *seg_path = pgs->path->segments;
  163.  
  164.     gx_path_scale_exp2_shared(pgs->path, log2_scale_x, log2_scale_y,
  165.                   seg_path == seg_clip ||
  166.                   seg_path == seg_view_clip ||
  167.                   seg_path == seg_effective_clip);
  168.     }
  169.     return 0;
  170. }
  171. private void
  172. scale_dash_pattern(gs_state * pgs, floatp scale)
  173. {
  174.     int i;
  175.  
  176.     for (i = 0; i < pgs->line_params.dash.pattern_size; ++i)
  177.     pgs->line_params.dash.pattern[i] *= scale;
  178.     pgs->line_params.dash.offset *= scale;
  179.     pgs->line_params.dash.pattern_length *= scale;
  180.     pgs->line_params.dash.init_dist_left *= scale;
  181.     if (pgs->line_params.dot_length_absolute)
  182.     pgs->line_params.dot_length *= scale;
  183. }
  184. private int
  185. alpha_buffer_init(gs_state * pgs, fixed extra_x, fixed extra_y, int alpha_bits)
  186. {
  187.     gx_device *dev = gs_currentdevice_inline(pgs);
  188.     int log2_alpha_bits = ilog2(alpha_bits);
  189.     gs_fixed_rect bbox;
  190.     gs_int_rect ibox;
  191.     uint width, raster, band_space;
  192.     uint height;
  193.     gs_log2_scale_point log2_scale;
  194.     gs_memory_t *mem;
  195.     gx_device_memory *mdev;
  196.  
  197.     log2_scale.x = log2_scale.y = log2_alpha_bits;
  198.     gx_path_bbox(pgs->path, &bbox);
  199.     ibox.p.x = fixed2int(bbox.p.x - extra_x) - 1;
  200.     ibox.p.y = fixed2int(bbox.p.y - extra_y) - 1;
  201.     ibox.q.x = fixed2int_ceiling(bbox.q.x + extra_x) + 1;
  202.     ibox.q.y = fixed2int_ceiling(bbox.q.y + extra_y) + 1;
  203.     width = (ibox.q.x - ibox.p.x) << log2_scale.x;
  204.     raster = bitmap_raster(width);
  205.     band_space = raster << log2_scale.y;
  206.     height = (abuf_nominal / band_space) << log2_scale.y;
  207.     if (height == 0)
  208.     height = 1 << log2_scale.y;
  209.     mem = pgs->memory;
  210.     mdev = gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  211.                "alpha_buffer_init");
  212.     if (mdev == 0)
  213.     return 0;        /* if no room, don't buffer */
  214.     gs_make_mem_abuf_device(mdev, mem, dev, &log2_scale,
  215.                 alpha_bits, ibox.p.x << log2_scale.x);
  216.     mdev->width = width;
  217.     mdev->height = height;
  218.     mdev->bitmap_memory = mem;
  219.     if ((*dev_proc(mdev, open_device)) ((gx_device *) mdev) < 0) {
  220.     /* No room for bits, punt. */
  221.     gs_free_object(mem, mdev, "alpha_buffer_init");
  222.     return 0;
  223.     }
  224.     gx_set_device_only(pgs, (gx_device *) mdev);
  225.     scale_paths(pgs, log2_scale.x, log2_scale.y, true);
  226.     return 1;
  227. }
  228.  
  229. /* Release an alpha buffer. */
  230. private void
  231. alpha_buffer_release(gs_state * pgs, bool newpath)
  232. {
  233.     gx_device_memory *mdev =
  234.     (gx_device_memory *) gs_currentdevice_inline(pgs);
  235.  
  236.     (*dev_proc(mdev, close_device)) ((gx_device *) mdev);
  237.     scale_paths(pgs, -mdev->log2_scale.x, -mdev->log2_scale.y,
  238.         !(newpath && !gx_path_is_shared(pgs->path)));
  239.     /* Reference counting will free mdev. */
  240.     gx_set_device_only(pgs, mdev->target);
  241. }
  242.  
  243. /* Fill the current path using a specified rule. */
  244. private int
  245. fill_with_rule(gs_state * pgs, int rule)
  246. {
  247.     int code;
  248.  
  249.     /* If we're inside a charpath, just merge the current path */
  250.     /* into the parent's path. */
  251.     if (pgs->in_charpath)
  252.     code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
  253.                      pgs->in_charpath);
  254.     else {
  255.     int abits, acode;
  256.  
  257.     gx_set_dev_color(pgs);
  258.     code = gs_state_color_load(pgs);
  259.     if (code < 0)
  260.         return code;
  261.     abits = alpha_buffer_bits(pgs);
  262.     if (abits > 1) {
  263.         acode = alpha_buffer_init(pgs, pgs->fill_adjust.x,
  264.                       pgs->fill_adjust.y, abits);
  265.         if (acode < 0)
  266.         return acode;
  267.     } else
  268.         acode = 0;
  269.     code = gx_fill_path(pgs->path, pgs->dev_color, pgs, rule,
  270.                 pgs->fill_adjust.x, pgs->fill_adjust.y);
  271.     if (acode > 0)
  272.         alpha_buffer_release(pgs, code >= 0);
  273.     if (code >= 0)
  274.         gs_newpath(pgs);
  275.  
  276.     }
  277.     return code;
  278. }
  279. /* Fill using the winding number rule */
  280. int
  281. gs_fill(gs_state * pgs)
  282. {
  283.     return fill_with_rule(pgs, gx_rule_winding_number);
  284. }
  285. /* Fill using the even/odd rule */
  286. int
  287. gs_eofill(gs_state * pgs)
  288. {
  289.     return fill_with_rule(pgs, gx_rule_even_odd);
  290. }
  291.  
  292. /* Stroke the current path */
  293. int
  294. gs_stroke(gs_state * pgs)
  295. {
  296.     int code;
  297.  
  298.     /*
  299.      * If we're inside a charpath, just merge the current path
  300.      * into the parent's path.
  301.      */
  302.     if (pgs->in_charpath) {
  303.     if (pgs->in_charpath == cpm_true_charpath) {
  304.         /*
  305.          * A stroke inside a true charpath should do the
  306.          * equivalent of strokepath.
  307.          */
  308.         code = gs_strokepath(pgs);
  309.         if (code < 0)
  310.         return code;
  311.     }
  312.     code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
  313.                      pgs->in_charpath);
  314.     } else {
  315.     int abits, acode;
  316.  
  317.     gx_set_dev_color(pgs);
  318.     code = gs_state_color_load(pgs);
  319.     if (code < 0)
  320.         return code;
  321.     abits = alpha_buffer_bits(pgs);
  322.     if (abits > 1) {
  323.         /*
  324.          * Expand the bounding box by the line width.
  325.          * This is expensive to compute, so we only do it
  326.          * if we know we're going to buffer.
  327.          */
  328.         float xxyy = fabs(pgs->ctm.xx) + fabs(pgs->ctm.yy);
  329.         float xyyx = fabs(pgs->ctm.xy) + fabs(pgs->ctm.yx);
  330.         float scale = 1 << (abits / 2);
  331.         float orig_width = gs_currentlinewidth(pgs);
  332.         float new_width = orig_width * scale;
  333.         fixed extra_adjust =
  334.         float2fixed(max(xxyy, xyyx) * new_width / 2);
  335.         float orig_flatness = gs_currentflat(pgs);
  336.         gx_path spath;
  337.  
  338.         /* Scale up the line width, dash pattern, and flatness. */
  339.         if (extra_adjust < fixed_1)
  340.         extra_adjust = fixed_1;
  341.         acode = alpha_buffer_init(pgs,
  342.                       pgs->fill_adjust.x + extra_adjust,
  343.                       pgs->fill_adjust.y + extra_adjust,
  344.                       abits);
  345.         if (acode < 0)
  346.         return acode;
  347.         gs_setlinewidth(pgs, new_width);
  348.         scale_dash_pattern(pgs, scale);
  349.         gs_setflat(pgs, orig_flatness * scale);
  350.         /*
  351.          * The alpha-buffer device requires that we fill the
  352.          * entire path as a single unit.
  353.          */
  354.         gx_path_init_local(&spath, pgs->memory);
  355.         code = gx_stroke_add(pgs->path, &spath, pgs);
  356.         gs_setlinewidth(pgs, orig_width);
  357.         scale_dash_pattern(pgs, 1.0 / scale);
  358.         if (code >= 0)
  359.         code = gx_fill_path(&spath, pgs->dev_color, pgs,
  360.                     gx_rule_winding_number,
  361.                     pgs->fill_adjust.x,
  362.                     pgs->fill_adjust.y);
  363.         gs_setflat(pgs, orig_flatness);
  364.         gx_path_free(&spath, "gs_stroke");
  365.         if (acode > 0)
  366.         alpha_buffer_release(pgs, code >= 0);
  367.     } else
  368.         code = gx_stroke_fill(pgs->path, pgs);
  369.     if (code >= 0)
  370.         gs_newpath(pgs);
  371.     }
  372.     return code;
  373. }
  374.  
  375. /* Compute the stroked outline of the current path */
  376. int
  377. gs_strokepath(gs_state * pgs)
  378. {
  379.     gx_path spath;
  380.     int code;
  381.  
  382.     gx_path_init_local(&spath, pgs->memory);
  383.     code = gx_stroke_add(pgs->path, &spath, pgs);
  384.     if (code < 0) {
  385.     gx_path_free(&spath, "gs_strokepath");
  386.     return code;
  387.     }
  388.     return gx_path_assign_free(pgs->path, &spath);
  389. }
  390.